package com.service.dao.enums;

import com.service.dao.model.MerchantCreditLine;
import lombok.Getter;
import lombok.NonNull;

import java.math.BigDecimal;

public enum StatementTypeEnum {

    REDUCE_BALANCE(10, "降低信用额度") {
        @Override
        public void doOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doOptNum(creditLine.getResidueNum(), num));
            creditLine.setCreditNum(doOptNum(creditLine.getCreditNum(), num));
        }

        @Override
        public BigDecimal doOptNum(BigDecimal startNum, BigDecimal num) {
            return startNum.subtract(num);
        }

        @Override
        public void doReturnOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setCreditNum(doReturnNum(creditLine.getCreditNum(), num));
            creditLine.setResidueNum(doReturnNum(creditLine.getResidueNum(), num));
        }

        @Override
        public BigDecimal doReturnNum(BigDecimal startNum, BigDecimal num) {
            return startNum.add(num);
        }
    },
    WRITE_OFF(11, "核销") {
        @Override
        public void doOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doOptNum(creditLine.getResidueNum(), num));
        }

        @Override
        public BigDecimal doOptNum(BigDecimal startNum, BigDecimal num) {
            return startNum.subtract(num);
        }

        @Override
        public void doReturnOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doReturnNum(creditLine.getResidueNum(), num));
        }

        @Override
        public BigDecimal doReturnNum(BigDecimal startNum, BigDecimal num) {
            return startNum.add(num);
        }

    },
    ADD_BALANCE(20, "提高信用额度") {
        @Override
        public void doOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doOptNum(creditLine.getResidueNum(), num));
            creditLine.setCreditNum(doOptNum(creditLine.getCreditNum(), num));
        }

        @Override
        public BigDecimal doOptNum(BigDecimal startNum, BigDecimal num) {
            return startNum.add(num);
        }

        @Override
        public void doReturnOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doReturnNum(creditLine.getResidueNum(), num));
            creditLine.setCreditNum(doReturnNum(creditLine.getCreditNum(), num));
        }

        @Override
        public BigDecimal doReturnNum(BigDecimal startNum, BigDecimal num) {
            return startNum.subtract(num);
        }
    },
    REPAYMENT(21, "还款") {
        @Override
        public void doOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doOptNum(creditLine.getResidueNum(), num));
        }

        @Override
        public BigDecimal doOptNum(BigDecimal startNum, BigDecimal num) {
            return startNum.add(num);
        }

        @Override
        public void doReturnOptStore(MerchantCreditLine creditLine, BigDecimal num) {
            creditLine.setResidueNum(doReturnNum(creditLine.getResidueNum(), num));
        }

        @Override
        public BigDecimal doReturnNum(BigDecimal startNum, BigDecimal num) {
            return startNum.subtract(num);
        }
    },

    ;

    @Getter
    private Integer code;
    @Getter
    private String msg;

    StatementTypeEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public abstract void doOptStore(@NonNull MerchantCreditLine creditLine, @NonNull BigDecimal num);

    public abstract BigDecimal doOptNum(@NonNull BigDecimal startNum, @NonNull BigDecimal num);

    public abstract void doReturnOptStore(@NonNull MerchantCreditLine creditLine, @NonNull BigDecimal num);

    public abstract BigDecimal doReturnNum(@NonNull BigDecimal startNum, @NonNull BigDecimal num);

    public static StatementTypeEnum valueOf(Integer code) {
        StatementTypeEnum[] jaTypes = values();
        for (StatementTypeEnum item : jaTypes) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return null;
    }


}
